package com.woniuxy.java106crm.controller;

import com.woniuxy.java106crm.annotation.RequireMethod;
import com.woniuxy.java106crm.entity.Order;
import com.woniuxy.java106crm.entity.OrderDetail;
import com.woniuxy.java106crm.request.ResponseResult;
import com.woniuxy.java106crm.service.OrderService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "客户订单管理API文档")
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Resource
    private OrderService orderService;

    @PreAuthorize("hasAuthority('客户信息管理')")
    @RequireMethod("查询客户订单")
    @RequestMapping("/findOrderByCid/{customerid}")
    public ResponseResult<List<Order>> findOrderByCid(@PathVariable("customerid") String customerid){
        log.debug("{}",customerid);
        return new ResponseResult<>(ResponseResult.OK,orderService.findOrderByCid(customerid));
    }

    @PreAuthorize("hasAuthority('客户信息管理')")
    @RequireMethod("删除客户订单")
    @DeleteMapping("/deleteById/{id}")
    public ResponseResult<Boolean> deleteById(@PathVariable("id") Integer id){
        orderService.deleteById(id);
        return new ResponseResult<>(ResponseResult.OK,true);
    }

    @PreAuthorize("hasAuthority('客户信息管理')")
    @RequireMethod("批量删除客户订单")
    @DeleteMapping("/batchedDelByIds/{ids}")
    public ResponseResult<Boolean> batchedDelByIds(@PathVariable("ids") String ids){
        orderService.batchedDelByIds(ids);
        return new ResponseResult<>(ResponseResult.OK,true);
    }

    @PreAuthorize("hasAuthority('客户信息管理')")
    @RequireMethod("编辑客户订单")
    @PostMapping("/updateOrder")
    public ResponseResult<Boolean> updateOrder(@RequestBody Order order){
        return new ResponseResult<>(ResponseResult.OK,orderService.updateOrder(order));
    }

    @PreAuthorize("hasAuthority('客户信息管理')")
    @RequireMethod("查询订单详细")
    @GetMapping("/findOrderDetail/{oid}")
    public ResponseResult<List<OrderDetail>> findOrderDetail(@PathVariable("oid") Integer oid){
        return new ResponseResult<>(ResponseResult.OK,orderService.findOrderDetail(oid));
    }
}
